<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="certificateNo">
        <el-input
          v-model="queryParams.merchantName"
          placeholder="经营者名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:SettleAccountInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:SettleAccountInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:SettleAccountInfo:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="营业执照名称" align="center" prop="merchantName" />
      <el-table-column label="店内照" prop="shopInsidePlacePicLocal"  width="180">
        <template slot-scope="scope">
          <img :src="scope.row.shopInsidePlacePicLocal" alt="" style="width: 100px;height: 100px">
        </template>
      </el-table-column>
      <el-table-column label="门头照" prop="shopGatePicLocal"  width="180">
        <template slot-scope="scope">
          <img :src="scope.row.shopGatePicLocal" alt="" style="width: 100px;height: 100px">
        </template>
      </el-table-column>
      <el-table-column label="收银台照片" prop="cashierDeskPicLocal"  width="180">
        <template slot-scope="scope">
          <img :src="scope.row.cashierDeskPicLocal" alt="" style="width: 100px;height: 100px">
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:SettleAccountInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:SettleAccountInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改结算信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺名称" prop="merchantName">
          <el-input v-model="form.merchantName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="总店商户Id" prop="headShopMerchantId">
          <el-input v-model="form.headShopMerchantId" placeholder="请输入总店商户Id" />
        </el-form-item>
        <el-form-item label="店内照">
          <el-upload
            :file-list="shopInsidePlacePicList"
            :on-change="(file,fileList)=>{return uploadChange(file,fileList)}"
            :on-remove="(file,fileList)=>{return handleRemove(file,fileList,'shopInsidePlacePic')}"
            action="#"
            ref="shopInsidePlacePic"
            list-type="picture-card"
            :on-preview="imgPreview"
            :data="form"
            :http-request="(file)=>{return shopInsidePlacePicHttpRequest(file)}"
            :auto-upload="false">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="imgVisible">
            <img width="100%" :src="imgPath" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="门头照">
          <el-upload
            :file-list="shopGatePicList"
            :on-change="(file,fileList)=>{return uploadChange(file,fileList)}"
            :on-remove="(file,fileList)=>{return handleRemove(file,fileList,'shopGatePic')}"
            action="#"
            ref="shopGatePic"
            list-type="picture-card"
            :on-preview="imgPreview"
            :data="form"
            :http-request="(file)=>{return shopGatePicHttpRequest(file)}"
            :auto-upload="false">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="imgVisible">
            <img width="100%" :src="imgPath" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="收银台照片">
          <el-upload
            :file-list="cashierDeskPicList"
            :on-change="(file,fileList)=>{return uploadChange(file,fileList)}"
            :on-remove="(file,fileList)=>{return handleRemove(file,fileList,'cashierDeskPic')}"
            action="#"
            ref="cashierDeskPic"
            list-type="picture-card"
            :on-preview="imgPreview"
            :data="form"
            :http-request="(file)=>{return cashierDeskPicHttpRequest(file)}"
            :auto-upload="false">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="imgVisible">
            <img width="100%" :src="imgPath" alt="">
          </el-dialog>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listShopInfo, getShopInfo, addShopInfo, updateShopInfo, delShopInfo } from "@/api/posp/shopInfo";

  export default {
    name: "shopInfo",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 法人信息表格数据
        shopInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          merchantName: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        imgVisible:false,
        imgPath:null,
        formData : new FormData(),
        shopInsidePlacePicList : [],
        shopGatePicList : [],
        cashierDeskPicList : []
      };
    },
    created() {
      this.getList();
      this.form.merchantId = this.$route.query.merchantId;
    },
    methods: {
      beforeUpload(file,fileList){
        let flag = true;
        let isLt2M = file.size / 1024  < 500;
        if (!isLt2M) {
          this.$modal.msgError("上传图片或视频大小不能超过500Kb'!")
          flag = false;
        }
        // 验证是否是图片
        if (file.type.indexOf("image/") == -1) {
          this.$modal.msgError("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。")
          flag = false;
        }
        if(flag){
          if (fileList.length > 1) {
            fileList.splice(0, 1)
          }
        }else{
          fileList.splice(fileList.length-1, 1);
        }
      },
      imgPreview(file) {
        this.imgPath = file.url;
        this.imgVisible = true;
      },
      shopInsidePlacePicHttpRequest(param){
        this.formData.append('shopInsidePlacePicFile', param.file);
      },
      shopGatePicHttpRequest(param){
        this.formData.append('shopGatePicFile', param.file);
      },
      cashierDeskPicHttpRequest(param){
        this.formData.append('cashierDeskPicFile', param.file);
      },
      handleRemove(file, fileList) {
        if(fileList.length == 0){
          if(funcName == 'shopInsidePlacePic'){
            this.form.shopInsidePlacePicLocal = ''
          }else if(funcName == 'shopGatePic'){
            this.form.shopGatePicLocal = ''
          }else if(funcName == 'cashierDeskPic'){
            this.form.cashierDeskPicLocal = ''
          }
        }
      },
      uploadChange(file, fileList) {
        this.beforeUpload(file.raw,fileList);
      },
      /** 查询结算信息列表 */
      getList() {
        this.loading = true;
        listShopInfo(this.queryParams).then(response => {
          this.shopInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: '',
          merchantName: '',
          shopInsidePlacePicLocal: '',
          shopGatePicLocal: '',
          cashierDeskPicLocal: '',
          headShopMerchantId: '',
          merchantId: ''
        };
        this.resetForm("form");
        this.formData = new FormData();
        this.shopInsidePlacePicList = [],
        this.shopGatePicList = [],
        this.cashierDeskPicList = []
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加门店信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getShopInfo(id).then(response => {
          this.form = response.data;
          if(this.form.shopInsidePlacePicLocal){
            let obj1 = {
              url:process.env.VUE_APP_BASE_API + this.form.shopInsidePlacePicLocal
            };
            this.shopInsidePlacePicList.push(obj1);
          }
          if(this.form.shopGatePicLocal){
            let obj1 = {
              url:process.env.VUE_APP_BASE_API + this.form.shopGatePicLocal
            };
            this.shopGatePicList.push(obj1);
          }
          if(this.form.cashierDeskPicLocal){
            let obj1 = {
              url:process.env.VUE_APP_BASE_API + this.form.cashierDeskPicLocal
            };
            this.cashierDeskPicList.push(obj1);
          }
          this.open = true;
          this.title = "修改门店信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.$refs.shopInsidePlacePic.submit();
            this.$refs.shopGatePic.submit();
            this.$refs.cashierDeskPic.submit();
            this.formData.append('id',this.form.id);
            this.formData.append('merchantName',this.form.merchantName);
            this.formData.append('headShopMerchantId',this.form.headShopMerchantId);
            this.formData.append('merchantId',this.$route.query.merchantId);
            if (this.form.id != null && this.form.id != '') {
              updateShopInfo(this.formData).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
            } else {
              addShopInfo(this.formData).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除数据项？').then(function() {
          return delBziLicenseInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    }
  };
</script>
